import type { IPageDataArgs } from '@/service/api/main/system/type'
import type { ISystemState } from './type'
import {
  updateStatusApi,
  deletePageDataByIdApi,
  patchPageDataApi,
  postNewPageDataApi,
  postPageDataApi,
  getDetailsByIdApi,
  userBlockByAccidsApi,
  userUnblockByAccidsApi
} from '@/service/api'
import { defineStore } from 'pinia'
import getMenuList from '@/utils/aysncMenu'
import { isCode } from '@/utils/iscode'
const useSystemStore = defineStore('system', {
  state: (): ISystemState => ({
    originalList:[],
    pageDataList: [],
    totalData:'',
    pageDataTotalCount: 0,
    imageUrl:'',
    searchData:[],
  }),
  actions: {
    //重置
    resetState(){
      this.$patch({pageDataList: [], pageDataTotalCount: 0,imageUrl:'' })
      this.$state.totalData=''
    },
     // 增
     addNewPageDataAction(pageName: string, userInfo: any) {
      return new Promise<any>((resolve, reject) => {
        postNewPageDataApi(pageName, userInfo)
          .then(res => {
            resolve(res)
          })
          .catch(err => {
            reject(err)
          })
      })
    },

     // 删
     deletePageDataAction(pageName: string, ids: Array<number>) {
      return new Promise<any>((resolve, reject) => {
        deletePageDataByIdApi(pageName, ids)
          .then(res => {
            resolve(res)
          })
          .catch(err => {
            reject(err)
          })
      })
    },

     // 改
     editPageDataAction(pageName: string, userInfo: any) {
      return new Promise<any>((resolve, reject) => {
        patchPageDataApi(pageName,userInfo)
          .then(res => {
            resolve(res)
          })
          .catch(err => {
            reject(err)
          })
      })
    },

    // 查
    async fetchPageDataAction(pageName: string, payload: IPageDataArgs,isMenu?) {
      const res = await postPageDataApi(pageName, payload)
      if(isCode(res)){
        if(res.result){
          const { list, count,url } = res.result
          this.$patch({ pageDataList: list, pageDataTotalCount: count,imageUrl:url })
          if(res.result?.totalData){
            this.totalData = res.result.totalData
          }
          if(isMenu){
           let menu= getMenuList(res.result)
           this.pageDataList = menu
           this.originalList =res.result
          }
        }else{
          const list = []
          const count = 0
          this.$patch({ pageDataList: list ,pageDataTotalCount: count})
        }
       
      }
     
    },

    //更改状态
    updateStatusAction(pageName:string,data:any){
      return new Promise<any>((resolve, reject) => {
        updateStatusApi(pageName,data)
          .then(res => {
            resolve(res)
          })
          .catch(err => {
            reject(err)
          })
      })
    },

    //根据Id查详情
    getDetailsById(pageName:string,id:number){
      return new Promise<any>((resolve, reject) => {
        getDetailsByIdApi(pageName,id)
          .then(res => {
            resolve(res)
          })
          .catch(err => {
            reject(err)
          })
      })
    },

    //封禁用户
    userBlock(pageName:string,data:any){
      return new Promise<any>((resolve, reject) => {
        userBlockByAccidsApi(pageName,data)
          .then(res => {
            resolve(res)
          })
          .catch(err => {
            reject(err)
          })
      })
    },

    //解封用户
    userUnblock(pageName:string,data:any){
      return new Promise<any>((resolve, reject) => {
        userUnblockByAccidsApi(pageName,data)
          .then(res => {
            resolve(res)
          })
          .catch(err => {
            reject(err)
          })
      })
    },

  },
  persist: {
    paths: ['']
  }
})


export default useSystemStore
